package com.oms.system.service.impl;

import com.oms.common.utils.date.DayTimeUtil;
import com.oms.system.domain.*;
import com.oms.system.mapper.OmsOrderMapper;
import com.oms.system.mapper.OmsOrderProductMapper;
import com.oms.system.service.IOmsOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("orderService")
public class OmsOrderServiceImpl implements IOmsOrderService {

    @Autowired
    private OmsOrderMapper orderMapper;
    @Autowired
    private OmsOrderProductMapper orderProductMapper;

    @Override
    public List<OmsOrder> getOrderList(OmsOrder order, List<Integer> ids, Boolean isAdmin) {
        OmsOrderExample example = new OmsOrderExample();
        OmsOrderExample.Criteria criteria = example.createCriteria();
        if(order.getOrderName() != null && !order.getOrderName().equals("")){
            criteria.andOrderNameLike("%"+order.getOrderName()+"%");
        }
        if(order.getOrderNumber() != null && !order.getOrderNumber().equals("")){
            criteria.andOrderNumberLike("%"+order.getOrderNumber()+"%");
        }
        if(order.getOrderStatus() != null && order.getOrderStatus() != 0){
            criteria.andOrderStatusEqualTo(order.getOrderStatus());
        }
        if(order.getOrderTime() != null){
            List<Date> orderTimes = DayTimeUtil.dayStartEndTime(order.getOrderTime());
            criteria.andOrderTimeBetween(orderTimes.get(0), orderTimes.get(1));
        }
        if(order.getSendTime() != null){
            List<Date> sendTimes = DayTimeUtil.dayStartEndTime(order.getSendTime());
            criteria.andSendTimeBetween(sendTimes.get(0), sendTimes.get(1));
        }
        List<OmsOrder> list = new ArrayList<>();
        if(ids.size() > 0 || isAdmin){
            if(ids.size() > 0){
                criteria.andOrderStatusIn(ids);
            }
            list = orderMapper.selectByExample(example);
        }
        return list;
    }

    @Override
    public OmsOrderProduct getOrderProductByUniCode(Long orderId, String uniCode, String userName) {
        OmsOrderExample example = new OmsOrderExample();
        OmsOrderExample.Criteria criteria = example.createCriteria();
        criteria.andUniCodeEqualTo(uniCode);
        List<OmsOrder> list = orderMapper.selectByExample(example);
        if(list != null && list.size() != 0){
            OmsOrderProductExample exampleProduct = new OmsOrderProductExample();
            OmsOrderProductExample.Criteria criteriaProduct = exampleProduct.createCriteria();
            criteriaProduct.andOrderIdEqualTo(orderId);
            List<OmsOrderProduct> omsOrderProducts = orderProductMapper.selectByExample(exampleProduct);
            if(omsOrderProducts != null && omsOrderProducts.size() > 0){
                OmsOrderProduct omsOrderProduct = omsOrderProducts.get(0);
                return omsOrderProduct;
            }
        }
        return null;
    }

    @Override
    public OrderGroup getOrderProductById(Long id) {
        OmsOrder order = orderMapper.selectByPrimaryKey(id);
        OmsOrderProductExample exampleProduct = new OmsOrderProductExample();
        OmsOrderProductExample.Criteria criteriaProduct = exampleProduct.createCriteria();
        criteriaProduct.andOrderIdEqualTo(id);
        List<OmsOrderProduct> omsOrderProducts = orderProductMapper.selectByExample(exampleProduct);
        OrderGroup orderGroup = new OrderGroup();
        if(omsOrderProducts != null && omsOrderProducts.size() > 0){
            OmsOrderProduct omsOrderProduct = omsOrderProducts.get(0);
            orderGroup.setOrderProduct(omsOrderProduct);
        }
        orderGroup.setOrder(order);
        return orderGroup;
    }

    @Override
    public void insertOrder(OmsOrder omsOrder, OmsOrderProduct omsOrderProduct) {
        orderMapper.insert(omsOrder);
        omsOrderProduct.setOrderId(omsOrder.getId());
        orderProductMapper.insert(omsOrderProduct);
    }

    @Override
    public void updateOrder(OmsOrder omsOrder, OmsOrderProduct omsOrderProduct) {
        orderMapper.updateByPrimaryKey(omsOrder);
        orderProductMapper.updateByPrimaryKey(omsOrderProduct);
    }

    @Override
    public void updateOrderStatus(Long id, Integer status) {
        OmsOrder order = new OmsOrder();
        order.setId(id);
        order.setOrderStatus(status);
        orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public void deleteOrder(List<Long> ids) {
        int size = ids.size();
        for (int i=0; i<size; i++){
            Long id = ids.get(i);
            orderMapper.deleteByPrimaryKey(id);
            OmsOrderProductExample exampleProduct = new OmsOrderProductExample();
            OmsOrderProductExample.Criteria criteriaProduct = exampleProduct.createCriteria();
            criteriaProduct.andOrderIdEqualTo(id);
            orderProductMapper.deleteByExample(exampleProduct);
        }
    }
}
